/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.techshop.dao.impl;

import com.techshop.dao.StoreDao;
import com.techshop.model.Store;
import java.util.List;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import redis.client.ValidatePhonePubSubClient;

/**
 *
 * @author Hai-PC
 */
public class StoreDaoImpl extends HibernateDaoSupport implements StoreDao
{
    @Override
    public Store getById(int storeId)
    {
        try
        {
            return (Store) getSession().get(Store.class, storeId);
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }

    @Override
    public List<Store> getAll()
    {
        try
        {
            return getSession().createQuery("FROM Store").list();
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }

    @Override
    public boolean insert(Store store)
    {
        try
        {
            getHibernateTemplate().save(store);
            ValidatePhonePubSubClient.changeStore();
            return true;
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }
    
    @Override
    public boolean update(Store store)
    {
        try
        {
            if (getById(store.getId()) == null)
            {
                return false;
            }
            else
            {
                getHibernateTemplate().update(getHibernateTemplate().merge(store));
                ValidatePhonePubSubClient.changeStore();
                return true;
            }
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }

    @Override
    public boolean delete(Store store)
    {
        try
        {
            if (getById(store.getId()) == null)
            {
                return false;
            }
            else
            {
                getHibernateTemplate().delete(getHibernateTemplate().merge(store));
                ValidatePhonePubSubClient.changeStore();
                return true;
            }
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }

    @Override
    public List<Store> getByProvinceAndDistrict(int provinceId, int districtId)
    {
        try
        {
            return getSession().createQuery("FROM Store WHERE district.id=:districtId AND province.id=:provinceId").setInteger("districtId", districtId).setInteger("provinceId", provinceId).list();
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }

    @Override
    public List<Store> getByProvince(int provinceId)
    {
        try
        {
            return getSession().createQuery("FROM Store WHERE province.id=:provinceId").setInteger("provinceId", provinceId).list();
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }

    @Override
    public List<Store> getByProvinceAndDistrictAndStore(int provinceId, int districtId, int storeId)
    {
        try
        {
            return getSession().createQuery("FROM Store WHERE province.id=:provinceId AND district.id=:districtId AND id=:storeId").setInteger("provinceId", provinceId).setInteger("districtId", districtId).setInteger("storeId", storeId).list();
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }
}